<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/7/24
 * Time: 16:32
 */

namespace app\statistical\controller;


use cmf\controller\AdminBaseController;
use think\Db;

class QuitClassController extends AdminBaseController
{
    /**
     *退班表
     */
    public function index()
    {

        $param=$this->request->param();
        $where = [
            'c.shop_id' => ['=', $this->admin_shop_id],
            'c.delete_time' => ['eq', 0]
        ];
        $where1=[];
        if (!empty($param['course_id'])) {
            //课程名称
            $where['c.course_id'] =$param['course_id'] ;
        }
        if (!empty($param['school_id']) && intval($param['school_id']) != 0) {
            //校区
            $where['c.school_id'] = intval($param['school_id']);
        }
        if (!empty($param['class_id'])) {
            //班级
            $where['c.id']= $param['class_id'];

        }
        $wheretime=[];
        $wheretime1=[];

        $homework_starttime = empty($param['homework_starttime']) ? 0 : strtotime($param['homework_starttime']);
        $homework_endtime   = empty($param['homework_endtime']) ? 0 : strtotime($param['homework_endtime']);
        if (!empty($homework_starttime) && !empty($homework_endtime)) {
            $wheretime['cl.create_time'] = [['>= time', $homework_starttime], ['<= time', $homework_endtime]];
            $wheretime1['o.update_time'] = [['>= time', $homework_starttime], ['<= time', $homework_endtime]];

        } else {
            if (!empty($homework_starttime)) {
                $wheretime['cl.create_time'] = ['>= time', $homework_starttime];
                $wheretime1['o.update_time'] = ['>= time', $homework_starttime];

            }
            if (!empty($homework_endtime)) {
                $wheretime['cl.create_time'] = ['<= time', $homework_endtime];
                $wheretime1['o.update_time'] = ['<= time', $homework_endtime];

            }
        }

           /*
             * 学校
             */
        $school_data=Db::name('school')
            ->where('shop_id', '=', $this->admin_shop_id)//多店铺
            ->where('delete_time', '=', 0)//未删除
            ->where('is_school', '=', 0)//1:地区 0:学校
            ->select();


        /*
         * 课程分类
         */
        $course_data=Db::name('course')
            ->where('shop_id', '=', $this->admin_shop_id)//多店铺
            ->where('delete_time', '=', 0)//未删除
            ->select();

        //班级
        $data_class = Db::name('class')->alias('c')->field('c.id cid,c.name cname,c.current_num,c.is_class,c.enable_class_ids')
            ->join('school sc', 'c.school_id=sc.id')
            ->join('course co', 'c.course_id=co.id')
            ->where($where)
            ->select();


        //报名的人数
        $data_all=Db::name('order')->alias('o')
                         ->join('class c','c.id=o.class_id')
                          ->where('o.order_status','neq','0')
                          ->where('o.order_status','neq','3')
                          ->where($where)
                         ->where($wheretime1)
                         ->select()->count('o.id');


    //退班的人数
    $data_tui = Db::name('order_refund')->alias('cl')
                     ->join('class c','c.id=cl.class_id')
                     ->where($where)->where($wheretime)
                     ->select()->count('cl.id');

        //如果$data_all==0
        if(empty($data_all))
        {
            $tui=20.00.'%';

        }else{
            $tui=$data_tui/$data_all;
            $tui=number_format($tui,2,'.','');
            $tui=($tui*100).'%';
        }

    $this->assign('data_all',json_encode($data_all));
    $this->assign('data_tui',json_encode($data_tui));
    $this->assign('school_data',$school_data);
    $this->assign('course_data',$course_data);
    $this->assign('param',$param);
    $this->assign('data_class',$data_class);
    $this->assign('tui',$tui);
    return $this->fetch();

    }

}